<template>
  <el-dialog
    :model-value="showVideoCourseDialog"
    width="1200"
    :close-on-click-modal="false"
    @close="closeDialog"
  >
    <template #header>
      <p style="color: black; font-weight: bold">视频课程选择器</p>
    </template>
    <div class="line"></div>

    <div class="dialog-content">
      <el-table
        ref="videoTableRef"
        row-key="id"
        :data="videoTableData"
        v-loading="tableLoading"
        @row-click="handleSelectedRow"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" v-if="!single" width="55" />
        <el-table-column prop="name" label="课程名称">
          <template #default="scope">
            <div v-if="!single">
              {{ scope.row.name }}
            </div>
            <div v-else>
              <el-radio-group v-model="selectedId">
                <el-radio :value="scope.row.id"></el-radio>
              </el-radio-group>
              {{ scope.row.name }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="封面图">
          <template #default="scope">
            <el-image
              class="table-img"
              preview-teleported
              :preview-src-list="[scope.row.img]"
              hide-on-click-modal
              v-if="scope.row.img"
              :src="scope.row.img"
              fit="cover"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="car" label="车型">
          <template #default="scope">
            <p>{{ scope.row.carModelList.map((item: any) => item.carName).join(',') }}</p>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="closeDialog" style="margin-right: 10px">关闭</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
  import { getcategoryList } from '@/api/questionbanklabel'

  const props = defineProps({
    showVideoCourseDialog: {
      type: Boolean,
      default: false
    },
    single: {
      type: Boolean
    },
    multipleSelectedData: {
      type: Array
    },
    singleSelectedData: {
      type: Object
    }
  })

  const emit = defineEmits(['closeDialog'])
  const selectCourseData = ref()
  const tableLoading = ref(false)
  const selectedId = ref(0)
  const videoTableData = ref([])
  const videoTableRef = ref()

  function closeDialog() {
    emit('closeDialog')
  }

  function handleConfirm() {
    emit('closeDialog', selectCourseData.value)
  }

  function handleSelectedRow(row: any) {
    selectedId.value = row.id
    selectCourseData.value = row
  }

  function handleSelectionChange(data: any[]) {
    selectCourseData.value = data
  }

  async function getVideoData() {
    try {
      tableLoading.value = true
      const res = await getcategoryList.getNewCateGoryId({
        pageNum: 1,
        pageSize: 1000,
        pageUrlType: '02'
      })
      if (res.code === '00000') {
        videoTableData.value = res.data.list[0]?.children

        nextTick(() => {
          const selectedData: any[] = props.multipleSelectedData || []
          selectedData.forEach((row: any) => {
            const match = videoTableData.value.find((item: any) => item.id === row.id)
            if (match) {
              videoTableRef.value?.toggleRowSelection(match, true)
            }
          })
        })
      }
    } catch (e) {
    } finally {
      tableLoading.value = false
    }
  }

  onMounted(() => {
    if (props.singleSelectedData?.id) {
      selectedId.value = props.singleSelectedData.id
    }
    getVideoData()
  })
</script>

<style scoped lang="scss">
  .line {
    height: 1px;
    width: 100%;
    background-color: #f0f0f0;
    margin-top: -20px;
    margin-bottom: 30px;
  }

  .flex-center {
    display: flex;
    align-items: center;
  }

  .table-img {
    width: 48px;
    height: 48px;
  }

  .dialog-content {
    height: 500px;
    overflow: auto;
  }

  .select-button {
    display: inline-block;
    padding: 6px 24px;
    border: 1px solid var(--el-color-primary);
    border-radius: 12px;
    color: var(--el-color-primary);
    background-color: #fff;
    font-size: 14px;
    cursor: pointer;
    text-align: center;
  }

  .select-button.active {
    background-color: var(--el-color-primary);
    color: white;
  }
</style>
